home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld Secrets (4th Edition)
/
Mac Secrets CD 4th Ed.toast
/
Apple Advanced Technologies
/
Apple Speech Technologies 1.5
/
PlainTalk Developer Info
/
Speech Recognition Manager SDK
/
SR Sample Code
/
Weapons Example
/
Weapons.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-04-11
|
6KB
|
309 lines
#include <Menus.h>
#include <Events.h>
#include <Quickdraw.h>
#include "Weapons.h"
#include "SpeechWeapons.h"
// Globals
Boolean gFinished = false;
WindowPtr gMainWindow = nil;
short gCurrentWeapon = kFirstItem;
// Prototypes
Boolean Initialize (void);
void EventLoop (void);
void Terminate (void);
void HandleMouseDown (EventRecord * event);
void HandleKeyDown (unsigned char keyChar);
void HandleMenuSelect (long menuChoice);
void HandleUpdate (WindowPtr window);
pascal OSErr DummyAEHandler (AppleEvent *theAEevt, AppleEvent* reply, long refcon);
pascal OSErr HandleQuitAE (AppleEvent *theAEevt, AppleEvent* reply, long refcon);
WindowPtr CreateMainWindow (void);
void DrawMainWindow (WindowPtr window);
void main (void)
{
if (Initialize ())
EventLoop ();
else
SysBeep (0);
Terminate ();
}
Boolean Initialize (void)
{
Boolean successfulStartup = false;
short index;
EventRecord event;
OSErr status = noErr;
InitGraf((Ptr) &qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(nil);
InitCursor();
for (index = 1; index <= 3; index++)
EventAvail(everyEvent, &event);
if (!status)
status = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
NewAEEventHandlerProc (DummyAEHandler), 0, false);
if (!status)
status = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
NewAEEventHandlerProc (DummyAEHandler), 0, false);
if (!status)
status = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
NewAEEventHandlerProc (DummyAEHandler), 0, false);
if (!status)
status = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
NewAEEventHandlerProc (HandleQuitAE), 0, false);
if (!status)
{
Handle menuBar = GetNewMBar(rMenuBar);
if (menuBar != nil)
{
SetMenuBar(menuBar);
DisposHandle(menuBar);
AddResMenu(GetMenuHandle(mApple), 'DRVR'); // add DA names to Apple menu
DrawMenuBar();
}
else
status = (ResError() ? ResError() : 1);
}
if (!status)
{
gMainWindow = CreateMainWindow ();
if (gMainWindow == nil)
status = 1;
}
MyInitSpeechRecognition ();
if (!status)
successfulStartup = true;
return successfulStartup;
}
void EventLoop (void)
{
while (!gFinished)
{
EventRecord event;
unsigned char keyChar;
if (WaitNextEvent(everyEvent, &event, 1, nil))
{
switch (event.what)
{
case mouseDown :
HandleMouseDown (&event);
break;
case keyDown :
keyChar = event.message & charCodeMask;
if (event.modifiers & cmdKey)
HandleMenuSelect (MenuKey(keyChar));
else
HandleKeyDown (keyChar);
break;
case updateEvt :
HandleUpdate ((WindowPtr) event.message);
break;
case kHighLevelEvent :
AEProcessAppleEvent(&event);
break;
default :
break;
}
}
}
}
void Terminate (void)
{
MyCleanupSpeechRecognition ();
}
void HandleMouseDown (EventRecord * event)
{
WindowPtr window;
short partCode = FindWindow (event->where, &window);
switch (partCode)
{
case inSysWindow :
SystemClick (event, window);
break;
case inContent:
break;
case inMenuBar :
HandleMenuSelect (MenuSelect(event->where));
break;
case inDrag :
DragWindow (window, event->where, &qd.screenBits.bounds);
break;
case inGoAway:
if (TrackGoAway (window, event->where))
gFinished = true;
break;
default :
break;
}
HiliteMenu (0);
}
void HandleKeyDown (unsigned char keyChar)
{
switch (keyChar)
{
case chLeft:
case chUp:
case chPageUp:
SetWeapon (--gCurrentWeapon);
break;
case chRight:
case chDown:
case chPageDown:
SetWeapon (++gCurrentWeapon);
break;
default:
break;
}
}
void HandleMenuSelect (long menuChoice)
{
short itemNum;
Str255 daName;
GrafPtr ourPort;
itemNum = LoWord(menuChoice);
switch (HiWord(menuChoice))
{
case mApple :
GetItem (GetMenuHandle(mApple), itemNum, daName);
GetPort (&ourPort);
OpenDeskAcc (daName);
SetPort (ourPort);
break;
case mFile :
// all we've got is quit
gFinished = true;
break;
default :
break;
}
}
void HandleUpdate (WindowPtr window)
{
if (window == gMainWindow)
{
BeginUpdate (window);
DrawMainWindow (window);
EndUpdate(window);
}
}
pascal OSErr DummyAEHandler (AppleEvent *theAEevt, AppleEvent* reply, long refcon)
{
return noErr;
}
pascal OSErr HandleQuitAE (AppleEvent *theAEevt, AppleEvent* reply, long refcon)
{
gFinished = true;
return noErr;
}
WindowPtr CreateMainWindow (void)
{
WindowPtr newWindow = nil;
newWindow = GetNewCWindow (rWindow, nil, (WindowPtr) -1);
if (newWindow)
SetPort (newWindow);
return newWindow;
}
void DrawMainWindow (WindowPtr window)
{
PicHandle pictHandle = nil;
Rect portRect = window->portRect;
SetPort (window);
pictHandle = (PicHandle) GetResource ('PICT', gCurrentWeapon);
if (pictHandle)
{
Rect picFrame = (*pictHandle)->picFrame;
short theID;
OSType theType;
Str255 name;
short width;
FontInfo fontInfo;
// Draw name
GetResInfo((Handle) pictHandle, &theID, &theType, name);
TextFont (geneva);
TextFace (bold);
TextSize (24);
GetFontInfo (&fontInfo);
width = StringWidth (name);
MoveTo ((portRect.left + portRect.right - width) / 2, 2 + fontInfo.ascent);
DrawString (name);
// normalize rect
OffsetRect (&picFrame, -picFrame.left, -picFrame.top);
// draw the picture in the center, without stretching it
OffsetRect (&picFrame,
(portRect.left + portRect.right) / 2 - (picFrame.left + picFrame.right) / 2,
2 + fontInfo.ascent + fontInfo.descent + fontInfo.leading);
DrawPicture (pictHandle, &picFrame);
ReleaseResource ((Handle) pictHandle);
}
}
void SetWeapon (short weaponID)
{
WindowPtr window = gMainWindow;
SetPort (window);
EraseRect (&window->portRect);
if (weaponID < kFirstItem)
weaponID = kLastItem;
else if (weaponID > kLastItem)
weaponID = kFirstItem;
gCurrentWeapon = weaponID;
DrawMainWindow (window);
}